// 获取当前时间戳（毫秒）
const timestampMilliseconds = () => Date.now();

// 获取当前时间戳（秒）
const timestampSeconds = () => Math.floor(Date.now() / 1000);

// 格式化时间
const formatDate = (dateString, format) => {
  const options = {
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit",
  };
  const date = new Date(dateString);
  const formattedDate = format.replace(/YYYY|MM|DD|HH|mm|ss/g, (match) => {
    const replacement = {
      YYYY: date.getFullYear().toString(),
      MM: ("0" + (date.getMonth() + 1)).slice(-2),
      DD: ("0" + date.getDate()).slice(-2),
      HH: ("0" + date.getHours()).slice(-2),
      mm: ("0" + date.getMinutes()).slice(-2),
      ss: ("0" + date.getSeconds()).slice(-2),
    }[match];
    return replacement || match;
  });
  return formattedDate;
};

/**
 * 获取指定日期是星期几
 * @param {Date} [date=new Date()] - 可选参数，默认为当前日期
 * @returns {string} - 返回星期几的字符串表示
 */
function getDayOfWeek(date = new Date()) {
  const daysOfWeek = [
    "星期日",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六",
  ];
  return daysOfWeek[date.getDay()];
}

// 返回时间的早上、下午和晚上
export const getTimePeriod = () => {
  const hours = new Date().getHours();

  if (hours > 0 && hours < 12) {
    return "早上";
  } else if (hours >= 12 && hours < 18) {
    return "下午";
  } else {
    return "晚上";
  }
};

// 返回月
export const getYear = (date = new Date()) => {
  return date.getFullYear();
};

// 返回月
export const getMonth = (date = new Date()) => {
  return date.getMonth() + 1;
};

// 返回日
export const getDay = (date = new Date()) => {
  return date.getDay();
};

// 返回时
export const getHours = (date = new Date()) => {
  return date.getHours();
};

// 返回分
export const getMinutes = (date = new Date()) => {
  return date.getMinutes();
};

// 返回秒
export const getSeconds = (date = new Date()) => {
  return date.getSeconds();
};

// 计算两个日期之间的时间差
export const calculateTimeDifference = (startDate, endDate) => {
  const start = new Date(startDate);
  const end = new Date(endDate);

  // 计算两个日期之间的毫秒差
  const diffTime = Math.abs(d1 - d2);

  // 将毫秒差转换为天数、小时数、分钟数和秒数
  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
  const diffHours = Math.ceil(diffTime / (1000 * 60 * 60));
  const diffMinutes = Math.ceil(diffTime / (1000 * 60));
  const diffSeconds = Math.ceil(diffTime / 1000);

  return [diffDays, diffHours, diffMinutes, diffSeconds];
};

// 计算两个日期之间的所有年份
export const getYearsBetween = (startDate, endDate) => {
  const start = new Date(startDate);
  const end = new Date(endDate);
  const years = [];

  if (start > end) {
    [start, end] = [end, start];
  }

  let current = new Date(start);
  while (current <= end) {
    const year = current.getFullYear();
    years.push(year);
    // 下一年
    current.setFullYear(year);
  }
  return years;
};

// 计算两个日期之间的所有月份
export const getMonthsBetween = (startDate, endDate) => {
  const start = new Date(startDate);
  const end = new Date(endDate);
  const months = [];

  if (start > end) {
    [start, end] = [end, start];
  }

  let current = new Date(start);
  while (current <= end) {
    const year = current.getFullYear();
    const month = current.getMonth() + 1;
    const fullMonth = ("0" + month).slice(-2);
    const _month = `${year}-${fullMonth}`;

    months.push(_month);
    // 下一个月
    current.setMonth(month);
  }
  return months;
};

export {
  getMonthsBetween,
  getYearsBetween,
  getYear,
  getTimePeriod,
  getDayOfWeek,
  timestampMilliseconds,
  timestampSeconds,
  formatDate,
};
